package com.imiku.controller;

import com.alibaba.fastjson.JSONObject;
import com.imiku.model.BlogInfo;
import com.imiku.service.BlogService;
import com.imiku.utils.JsonUtils;
import com.imiku.vo.BlogVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;

/**
 * Created by Lenovo on 2017/3/21.
 */
@RestController
@RequestMapping("/blog")
public class BlogController {

    @Autowired
    private BlogService blogService;

    @RequestMapping(value = "/findByPage", produces = "application/json")
    public String findBlogs(BlogVo blogVo) {
        Page<BlogInfo> page = blogService.findBlogs(blogVo);
        JSONObject jsonObject = JsonUtils.getJsonObject(1, "成功");
        jsonObject.put("page", page);
        return jsonObject.toJSONString();
    }

    @RequestMapping("/details/{blogId}")
    public JSONObject details(@PathVariable String blogId) {
        BlogInfo blogInfo = blogService.queryBlogById(blogId);
        JSONObject jsonObject = JsonUtils.getJsonObject(1, "成功");
        jsonObject.put("info", blogInfo);
        return jsonObject;
    }


    @RequestMapping("/save")
    public String save(BlogInfo blogInfo) {
        return blogService.save(blogInfo);
    }

    @RequestMapping("/update")
    public String update(BlogInfo blogInfo) throws UnsupportedEncodingException {
        return blogService.update(blogInfo);
    }
}
